_ = require('underscore')


#去除字符两边空格
str_trim = exports.trim = (val) ->
  val = '' + val
  if val!=null && val != undefined && val != ""
    return val.replace(/(^\s*)|(\s*$)/g, "")
  else
    return val


exports.check_value = (v1,v2) ->
  flag = true
  if _.isArray(v1)
    for name,i in v1
      if !str_trim(v1[i]) || !str_trim(v2[i])
        flag = false
        break
  else
    if !str_trim(v1) || !str_trim(v2)
      flag = false
  return flag





exports.parse_name_and_id = (names,ids) ->
  values = []
  if _.isArray(names)
    for name,i in names
      param = {}
      param.full_name = name
      param.id_number = ids[i]
      if param.full_name or param.id_number
        values.push param
  else
    param = {}
    param.full_name = names
    param.id_number = ids
    if param.full_name or param.id_number
      values.push param
  return values

exports.check_org_code = (codes) ->
  return true

exports.parse_name_and_code = (names,codes) ->
  values = []
  if _.isArray(names)
    for name,i in names
      param = {}
      param.name = name
      param.code = codes[i]
      if param.name or param.code
        values.push param
  else
    param = {}
    param.name = names
    param.code = codes
    if param.name or param.code
      values.push param
  return values


exports.str2arr = (str,delimiter) ->
  values = []
  if str
    str = str.toString()
    if str.indexOf(delimiter) != -1
      str_arr = str.split(delimiter)
      for item,i in str_arr
        item = str_arr[i]
        if item
          values.push item
    else
      if str
        values.push str
  return values


exports.validIdCard = (code) ->
  return true


exports.parse_date_str = (date_str) ->
  return new Date(Date.parse(date_str.replace(/-/g,"/")))


exports.verify_mobile = (mobile,code,req) ->
  data = req.session.mobile_verify
  unless data?[data.length-1]?.code == code and data?[data.length-1]?.mobile == mobile
    return "验证码错误或已过期"
  return null